package io.finer.erp.jeecg.approve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.finer.erp.jeecg.approve.entity.ApproveFlow;
import io.finer.erp.jeecg.approve.entity.ApproveFlowNode;
import io.finer.erp.jeecg.approve.mapper.ApproveFlowMapper;
import io.finer.erp.jeecg.approve.service.ApproveFlowNodeService;
import io.finer.erp.jeecg.approve.service.ApproveFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.finer.erp.jeecg.approve.vo.ApproveFlowNodePage;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequestLine;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 审批流 服务实现类
 * </p>
 *
 * @author yaoyifan
 * @since 2021-04-14
 */
@Service
public class ApproveFlowServiceImpl extends ServiceImpl<ApproveFlowMapper, ApproveFlow> implements ApproveFlowService {

    @Autowired
    ApproveFlowNodeService approveFlowNodeService;

    @Override
    @Transactional
    public void saveMain(ApproveFlow approveFlow, List<ApproveFlowNodePage> approveFlowNodePageList) {
        this.baseMapper.insert(approveFlow);
        if(approveFlowNodePageList != null && approveFlowNodePageList.size() > 0){
            for(ApproveFlowNodePage entityPage: approveFlowNodePageList){
                entityPage.setApproveFlowId(approveFlow.getId());
                ApproveFlowNode approveFlowNode = new ApproveFlowNode();
                BeanUtils.copyProperties(entityPage, approveFlowNode);
                approveFlowNodeService.saveMain(approveFlowNode, entityPage.getApproveMemberList());
            }
        }
    }

    @Override
    @Transactional
    public void updateMain(ApproveFlow approveFlow, List<ApproveFlowNodePage> approveFlowNodePageList) {
        this.baseMapper.updateById(approveFlow);
        QueryWrapper<ApproveFlowNode> queryWrapper = new QueryWrapper<>();
        approveFlowNodeService.remove(queryWrapper.eq("approve_flow_id",approveFlow.getId()));
        if (approveFlowNodePageList != null && approveFlowNodePageList.size() > 0){
            for(ApproveFlowNodePage entityPage: approveFlowNodePageList){
                entityPage.setApproveFlowId(approveFlow.getId());
                ApproveFlowNode approveFlowNode = new ApproveFlowNode();
                BeanUtils.copyProperties(entityPage, approveFlowNode);
                approveFlowNodeService.saveMain(approveFlowNode, entityPage.getApproveMemberList());
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {

    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {

    }
}
